#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFER_SIZE 1024

int main(int argc,char * argv[])
{
    int fd;
    char *buf;
    ssize_t ret;
    int len =10;
    char* content = NULL;
    int content_size = 0;

    if(argc !=2)
    {
        printf("Usage:%s<filename>\n",argv[0]);
    }
    //打开文件
    fd = open(argv[1],O_RDONLY);
    if(fd == -1)
    {
        perror("open");
        return 1;
    }

    //分配缓冲区内存
    buf =(char *)malloc(BUFFER_SIZE);
    if(buf == NULL)
    {
        perror("malloc");
        close(fd);
        return 1;
    }


    while(len !=0 && (ret = read(fd,buf,len)) !=0)
    {
        if(ret == -1){
            if(errno == EINTR)
                continue;
            perror("read");
            break;
        }
         len -= ret;
         buf += ret;
    }

    printf("len=%d\n",len);
    printf("buf=%s\n",buf);

    return 0;
}
